{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-08T07:02:31.460600Z",
     "start_time": "2025-01-08T07:02:31.457587Z"
    }
   },
   "source": [
    "# 导入库\n",
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Series: 类似于一维数组，可以包含不同的数据类型，可以索引，切片等操作。",
   "id": "1acc4c8f585ef1ab"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:36:30.680990Z",
     "start_time": "2025-01-08T06:36:30.677586Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建Series\n",
    "# 默认索引从0开始，可以指定索引\n",
    "ser_obj = pd.Series(range(10, 20))\n",
    "\n",
    "# 打印输出带有类型信息的Series\n",
    "print(ser_obj)"
   ],
   "id": "7f2a6aa9c2896670",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "3    13\n",
      "4    14\n",
      "5    15\n",
      "6    16\n",
      "7    17\n",
      "8    18\n",
      "9    19\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:51:17.981200Z",
     "start_time": "2025-01-08T06:51:17.978063Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# .values属性可以获取Series中的数据,ndarray类型\n",
    "print(ser_obj.values)\n",
    "print(type(ser_obj.values))\n",
    "\n",
    "# .index属性可以获取Series中的索引,Index类型\n",
    "print(ser_obj.index)\n",
    "print(type(ser_obj.index))"
   ],
   "id": "94d5f187bcf2fa87",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 11 12 13 14 15 16 17 18 19]\n",
      "<class 'numpy.ndarray'>\n",
      "RangeIndex(start=0, stop=10, step=1)\n",
      "<class 'pandas.core.indexes.range.RangeIndex'>\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:52:06.693765Z",
     "start_time": "2025-01-08T06:52:06.690765Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获得数据\n",
    "print(ser_obj[0])\n",
    "\n",
    "# 超出索引范围会报错"
   ],
   "id": "d6f6854656009d48",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:53:04.483073Z",
     "start_time": "2025-01-08T06:53:04.476874Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# **运算符可以对Series进行运算\n",
    "print(ser_obj * 2)\n",
    "\n",
    "# 比较运算返回布尔值Series\n",
    "print(ser_obj > 15)"
   ],
   "id": "442f44a02d5f799b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    20\n",
      "1    22\n",
      "2    24\n",
      "3    26\n",
      "4    28\n",
      "5    30\n",
      "6    32\n",
      "7    34\n",
      "8    36\n",
      "9    38\n",
      "dtype: int64\n",
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5    False\n",
      "6     True\n",
      "7     True\n",
      "8     True\n",
      "9     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:56:03.467744Z",
     "start_time": "2025-01-08T06:56:03.460743Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#字典变为series，索引是字典的key，value是字典的value\n",
    "year_data = {2001: 17.8, 2005: 20.1, 2003: 16.5}\n",
    "ser_obj2 = pd.Series(year_data)\n",
    "print(ser_obj2)\n",
    "\n",
    "# 索引是字典的key\n",
    "print(ser_obj2.index)\n",
    "\n",
    "# 值是字典的value\n",
    "print(ser_obj2[2001])\n",
    "ser_obj2.values"
   ],
   "id": "b902008d092410cf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2001    17.8\n",
      "2005    20.1\n",
      "2003    16.5\n",
      "dtype: float64\n",
      "Index([2001, 2005, 2003], dtype='int64')\n",
      "17.8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([17.8, 20.1, 16.5])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:57:34.139388Z",
     "start_time": "2025-01-08T06:57:34.136781Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Series名字\n",
    "ser_obj2.name = 'GDP'\n",
    "\n",
    "# 索引名字\n",
    "ser_obj2.index.name = 'year'"
   ],
   "id": "da1b1117cad1db1f",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# DataFrame: 类似于二维数组，可以包含不同的数据类型，可以索引，切片等操作。",
   "id": "fb929228468452d2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:02:42.558610Z",
     "start_time": "2025-01-08T07:02:42.553603Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过ndarray创建DataFrame\n",
    "# 列索引默认从0开始，行索引默认从0开始\n",
    "t = pd.DataFrame(np.arange(12).reshape((3, 4)))\n",
    "print(t)"
   ],
   "id": "981e7db54127646b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0  1   2   3\n",
      "0  0  1   2   3\n",
      "1  4  5   6   7\n",
      "2  8  9  10  11\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:04:24.960246Z",
     "start_time": "2025-01-08T07:04:24.956544Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 单独取某一行\n",
    "# .loc[]:列名或位置索引，行名或位置索引\n",
    "print(t.loc[1])\n",
    "\n",
    "# 取某一列\n",
    "# .iloc[]：列位置索引，行位置索引\n",
    "print(t.iloc[:, 1])"
   ],
   "id": "26407875fb7a0966",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    4\n",
      "1    5\n",
      "2    6\n",
      "3    7\n",
      "Name: 1, dtype: int64\n",
      "0    1\n",
      "1    5\n",
      "2    9\n",
      "Name: 1, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:05:28.245596Z",
     "start_time": "2025-01-08T07:05:28.241107Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 列表套字典变为DataFrame\n",
    "d2 = [{\"name\": \"xiaohong\", \"age\": 32, \"tel\": 10010},\n",
    "      {\"name\": \"xiaogang\", \"tel\": 10000},\n",
    "      {\"name\": \"xiaowang\", \"age\": 22}]\n",
    "df6 = pd.DataFrame(d2)\n",
    "\n",
    "#缺失值用NaN填充\n",
    "print(df6)"
   ],
   "id": "8cd76c769efd5412",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       name   age      tel\n",
      "0  xiaohong  32.0  10010.0\n",
      "1  xiaogang   NaN  10000.0\n",
      "2  xiaowang  22.0      NaN\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:06:07.530871Z",
     "start_time": "2025-01-08T07:06:07.526040Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# index:\n",
    "pd.Series(1, index=list(range(3, 7)), dtype='float32')"
   ],
   "id": "953b942517e0dfa0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3    1.0\n",
       "4    1.0\n",
       "5    1.0\n",
       "6    1.0\n",
       "dtype: float32"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:06:54.367441Z",
     "start_time": "2025-01-08T07:06:54.362936Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame名字中不同列可以是不同的数据类型,同一列必须是一个数据类型\n",
    "dict_data = {'A': 1,\n",
    "             'B': pd.Timestamp('20190926'),\n",
    "             'C': pd.Series(1, index=list(range(4)), dtype='float32'),\n",
    "             'D': np.array([1, 2, 3, 4], dtype='int32'),\n",
    "             'E': [\"Python\", \"Java\", \"C++\", \"C\"],\n",
    "             'F': 'wangdao'}\n",
    "\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "print(df_obj2)"
   ],
   "id": "b5cd92f41417e505",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  4       C  wangdao\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:08:27.886319Z",
     "start_time": "2025-01-08T07:08:27.882362Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 不能单独修改某个索引\n",
    "\n",
    "# 行索引\n",
    "print(df_obj2.index)\n",
    "\n",
    "# 列索引\n",
    "print(df_obj2.columns)\n",
    "\n",
    "# 每列的数据类型\n",
    "print(df_obj2.dtypes)"
   ],
   "id": "1b3fcd70f5b035a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index([0, 1, 2, 3], dtype='int64')\n",
      "Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')\n",
      "A            int64\n",
      "B    datetime64[s]\n",
      "C          float32\n",
      "D            int32\n",
      "E           object\n",
      "F           object\n",
      "dtype: object\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:10:07.276093Z",
     "start_time": "2025-01-08T07:10:07.270787Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 日期索引\n",
    "# #默认freq='D'，即天频率，periods=6，即生成6个日期\n",
    "dates = pd.date_range('20130101', periods=6)\n",
    "\n",
    "# columns: 列索引\n",
    "df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))\n",
    "\n",
    "print(df)\n",
    "print(df.index)"
   ],
   "id": "5d6e8f0e1af3b4b6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   A         B         C         D\n",
      "2013-01-01 -1.587936  0.510914 -1.392805  0.719344\n",
      "2013-01-02 -0.049619 -1.174838 -1.875946 -1.014853\n",
      "2013-01-03  0.422026 -0.187214  1.214261 -0.451324\n",
      "2013-01-04  0.520976  0.274147 -0.325357  1.105048\n",
      "2013-01-05 -0.892414  0.506346 -0.934722 -1.205128\n",
      "2013-01-06  0.244669  1.358091 -0.327241  1.313509\n",
      "DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',\n",
      "               '2013-01-05', '2013-01-06'],\n",
      "              dtype='datetime64[ns]', freq='D')\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:12:37.967471Z",
     "start_time": "2025-01-08T07:12:37.962470Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#增加列数据，列名是自定义的\n",
    "df_obj2['G'] = df_obj2['D'] + 4\n",
    "print(df_obj2.head())"
   ],
   "id": "2c39bfe72a0539c3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F  G\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao  5\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao  6\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao  7\n",
      "3  1 2019-09-26  1.0  4       C  wangdao  8\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:12:44.041335Z",
     "start_time": "2025-01-08T07:12:44.035814Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 删除列\n",
    "del (df_obj2['G'])\n",
    "print(df_obj2.head())\n"
   ],
   "id": "afdb9bf8aff62ae0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  4       C  wangdao\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "c0d7fb61b5d92887"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
